package com.watson.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.watson.R;
import com.watson.utils.LogUtils;

import java.util.ArrayList;
import java.util.Map;

public class Img2Adapter extends RecyclerView.Adapter<Img2Adapter.ItemViewHolder> {
    private ItemClickListener mItemClick;
    private ArrayList<Map> mList;
    private Context mContext;
    private int type = 0;

    public Img2Adapter(Context mContext, ArrayList<Map> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    public void setData(ArrayList<Map> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    public void setType(int type) {
        this.type = type;
        notifyDataSetChanged();
    }

    public void setItemClickListener(ItemClickListener listener) {
        this.mItemClick = listener;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemViewHolder itemViewHolder;
        itemViewHolder = new ItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_img2, parent, false));
        return itemViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {

        if (type == 0) {
            if (mList.size() > position) {
//            holder.imgAdd.setImageBitmap((Bitmap) mList.get(position).get("img"));
                Glide.with(mContext)
                        .load(mList.get(position).get("img")).error(R.mipmap.icon_add)
                        .into(holder.imgAdd);
                holder.imgDel.setVisibility(View.VISIBLE);
                holder.imgDel.setOnClickListener(v -> {
                    if (mItemClick != null) {
                        mItemClick.delClick(position, mList);
                    }
                });
            } else {
                holder.imgAdd.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.icon_add));
                holder.imgDel.setVisibility(View.GONE);
            }
        } else {
            Glide.with(mContext)
                    .load(mList.get(position).get("img")).error(R.mipmap.icon_add)
                    .into(holder.imgAdd);
            holder.imgDel.setVisibility(View.GONE);
        }

        holder.imgAdd.setOnClickListener(v -> {
            if (mList.size() > position) {
                if (mItemClick != null) {
                    mItemClick.getClick(position, mList);
                }
            } else {
                if (mItemClick != null) {
                    mItemClick.addClick(position, mList);
                }
            }
        });

        LogUtils.d("TAG", mList.toString() + "");
    }

    @Override
    public int getItemCount() {
        if (type == 1) {
            return mList.size();
        } else {
            LogUtils.d("TAG", mList.size() + "");
            if (mList.size() == 3) {
                return mList.size();
            } else {
                return mList.size() + 1;
            }
        }
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        private ImageView imgAdd, imgDel;

        public ItemViewHolder(View itemView) {
            super(itemView);
            imgAdd = itemView.findViewById(R.id.imgAdd);
            imgDel = itemView.findViewById(R.id.imgDel);
        }
    }

    public interface ItemClickListener {
        void addClick(int position, ArrayList<Map> mList);

        void delClick(int position, ArrayList<Map> mList);

        void getClick(int position, ArrayList<Map> mList);
    }
}
